<?php
/**
 * Created by PhpStorm.
 * User: Xavier
 * Date: 3/14/14
 * Time: 12:43 AM
 */
/*
 * Plugin Name: JW Options
 * Plugin URI: http://org.abc.com
 * Description: Options page for a theme.
 * Version: 1.0
 * Author: Xavier Huynh
 * Author URI: http://org.abc.com
 */

//Step 2
class JW_Options{
    //Step 11
    public $options;
    //Step 11
    public function  __construct(){
        //Step 6

        //Step 12
        //delete_option('jw_plugin_options');
        $this->options = get_option('jw_plugin_options');
        //Step 12
        $this->register_settings_and_fields();

        // End step 6
    }
    public function  add_menu_page(){
//        add_options_page('Theme Options', 'Theme Options', 'administrator', __FILE__, $this->somefunc);
        //or
        add_options_page('Theme Options', 'Theme Options', 'administrator', __FILE__, array(JW_Options, 'display_options_page'));

    }

    public function display_options_page(){
        ?>
            <div class="wrap">
                <?php get_screen_icon(); ?>
                <h2>My Theme Options</h2>
                <?php
                    $o = get_option('jw_plugin_options');
                    echo '<pre>';
                    print_r($o);
                    echo '</pre>';
                ?>

                <form method="post" enctype="multipart/form-data" action="options.php">

                    <?php //Step 7 ?>
                    <?php settings_fields('jw_plugin_options'); ?>
                    <?php do_settings_sections(__FILE__); ?>

                    <?php //Step 10 ?>
                    <p class="submit">
                        <input type="submit" name="submit" class="button-primary" value="Save Changes"/>
                    </p>
                    <?php //End Step 10 ?>
                    <?php //End Step 7 ?>
                </form>
            </div>
        <?php
    }

    //Step 4
    public function register_settings_and_fields(){
        register_setting('jw_plugin_options','jw_plugin_options',array($this, 'jw_validate_settings')); //3rd param = optional cb
        add_settings_section('jw_main_section','Main Settings',array($this, 'jw_main_section_cb'),__FILE__);//id, title of section, cb, which page?
        add_settings_field('jw_banner_heading', 'Banner Heading: ',array($this, 'jw_banner_heading_setting'),__FILE__, 'jw_main_section');
        add_settings_field('jw_logo', 'Your Logo: ',array($this, 'jw_logo_setting'),__FILE__, 'jw_main_section');
        //Step 16
        add_settings_field('jw_color_scheme', 'Your Desired Color Scheme: ',array($this, 'jw_color_scheme_setting'),__FILE__, 'jw_main_section');
        //End Step 16
    }

    //Step 8
    public function jw_main_section_cb(){
        //optional
    }
    //End Step 8

    //Step 14
        public function jw_validate_settings($plugin_options){
            if(!empty($_FILES['jw_logo_upload']['tmp_name'])){
                $override = array('test_form' => false);
                $file = wp_handle_upload($_FILES['jw_logo_upload'],$override);
                $plugin_options['jw_logo'] = $file['url'];
            }
            else{
                $plugin_options['jw_logo'] = $this->options['jw_logo'];
            }
            return $plugin_options;
        }
    //End Step 14
    /*
     * Inputs
     */
    //Banner Heading
    public function jw_banner_heading_setting(){
//        echo '<input />';
        //Step 13
        echo "<input name='jw_plugin_options[jw_banner_heading]' type='text' value='{$this->options['jw_banner_heading']}' />";
        //End Step 13
    }

    //End Step 4

    //Step 9
    public function jw_logo_setting(){
        echo '<input type="file" name="jw_logo_upload" /><br/><br/>';
        // Step 15
        if(isset($this->options['jw_logo'])){
            echo "<img src='{$this->options['jw_logo']}' alt='' />";
        }
        //End Step 15
    }
    //End Step 9

    //Step 17
    public function jw_color_scheme_setting(){
        $items = array('Red','Green','Blue','Yellow','Black','White');
        echo "<select name='jw_plugin_options[jw_color_scheme]'>";
        foreach($items as $item){
            $selected = ($this->options['jw_color_scheme'] === $item) ? 'selected="selected"':'';
            echo "<option value='$item' $selected>$item</option>";
        }
        echo "</select>";
    }
    //End Step 17

}
//End Step 2


// Step 1
//add_action('admin_menu',function(){
//   add_options_page('Theme Options', 'Theme Options', 'administrator', __FILE__, function(){
//       ?>
<!--       <div class="wrap">-->
<!--           --><?php //get_screen_icon(); ?>
<!--           <h2>My Theme Options</h2>-->
<!---->
<!--           <form method="post" enctype="multipart/form-data" action="options.php">-->
<!--               <input />-->
<!--           </form>-->
<!--       </div>-->
<!--       --><?php
//   });
//});

//End Step 1

//Step 3
add_action('admin_menu',function(){
   JW_Options::add_menu_page();
});

//End Step 3

//Step 5
add_action('admin_init',function(){
    new JW_Options();
});
//End Step 5